package com.example.Controller;

import com.example.entity.Adminusers;
import com.example.service.AdminuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;

@Controller
public class LoginCtrl {
    //@Resource
    @Autowired
    AdminuserService adminuserService;

    @GetMapping("/toLoginPage")
    public String toLogin(){
        return "login";
    }

    @RequestMapping(value = "doLogin",method = RequestMethod.POST)
    //@PostMapping("/doLogin")
    public String doLogin(Adminusers adminuser, Model model, HttpSession session){
        System.out.println(adminuser.getAduname() + "--=+++--"+adminuser.getAdupwd());

       // 用户名 密码进行验证
        Adminusers user =  adminuserService.geAdminUser(adminuser);
       if(user == null){
           model.addAttribute("msg","用户名或密码不正确");
           return "login";
       }else{
           session.setAttribute("uName",adminuser.getAduname());
           return "redirect:/admin/goods";
       }
    }


//    public String doLogin(String aduName,@RequestParam("aduPass") String aduPas){
//        System.out.println(aduName + "----"+aduPas);
//        return "one";
//    }
@GetMapping("logout")
public String doLogout(HttpSession session, SessionStatus sessionStatus){
    // 只是让session失效 但session仍然存在
    session.invalidate();
    sessionStatus.setComplete();
    return "login";
}

}
